package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.FocusUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class FocusUserApiImpl implements FocusUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveFocus(FocusUser focusUser) {
        mongoTemplate.save(focusUser);
    }

    @Override
    public void deleteFocus(FocusUser focusUser) {
        Query query = Query.query(Criteria.where("userId").is(focusUser.getUserId())
                .and("publishUserId").is(focusUser.getPublishUserId())
                .and("commentType").is(focusUser.getCommentType()));
        mongoTemplate.remove(query, FocusUser.class);
    }

    //判断是否关注
    @Override
    public Boolean hasFocus(Long userId, Long publishUserId, CommentType focus) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("publishUserId").is(publishUserId)
                .and("commentType").is(focus.getType()));
        return mongoTemplate.exists(query, FocusUser.class);
    }
}
